package com.atguigu.tingshu.album.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.atguigu.tingshu.album.mapper.BaseCategory1Mapper;
import com.atguigu.tingshu.album.mapper.BaseCategory2Mapper;
import com.atguigu.tingshu.album.mapper.BaseCategory3Mapper;
import com.atguigu.tingshu.album.mapper.BaseCategoryViewMapper;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.BaseAttribute;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategory3;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.vo.album.AlbumCategoryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1>
        implements BaseCategoryService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;

    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;

    /**
     * 获取所有分类信息
     *
     * @return
     * @author daiyuling
     * @date 2025/10/20
     */
    @Override
    public List<AlbumCategoryVo> getAllBaseCategoryList() {
        List<BaseCategoryView> rows = baseCategoryViewMapper.selectList(null);
        Map<Long, AlbumCategoryVo> l1Map = new LinkedHashMap<>();
        Map<Long, AlbumCategoryVo> l2Map = new LinkedHashMap<>();

        for (BaseCategoryView row : rows) {
            // L1
            AlbumCategoryVo l1Vo = l1Map.computeIfAbsent(row.getCategory1Id(), id -> {
                AlbumCategoryVo vo = new AlbumCategoryVo();
                vo.setCategoryId(id);
                vo.setCategoryName(row.getCategory1Name());
                vo.setCategoryChild(new ArrayList<>());
                return vo;
            });

            // L2
            AlbumCategoryVo l2Vo = l2Map.computeIfAbsent(row.getCategory2Id(), id -> {
                AlbumCategoryVo vo = new AlbumCategoryVo();
                vo.setCategoryId(id);
                vo.setCategoryName(row.getCategory2Name());
                vo.setCategoryChild(new ArrayList<>());
                l1Vo.getCategoryChild().add(vo);
                return vo;
            });

            // L3
            AlbumCategoryVo l3Vo = new AlbumCategoryVo();
            l3Vo.setCategoryId(row.getCategory3Id());
            l3Vo.setCategoryName(row.getCategory3Name());
            l2Vo.getCategoryChild().add(l3Vo);
        }
        return new ArrayList<>(l1Map.values());
    }

    /**
     * 获取根据一级分类id获取分类信息
     *
     * @param categoryId 一级分类ID
     * @return
     * @author daiyuling
     * @date 2025/10/20
     */
    @Override
    public AlbumCategoryVo getBaseCategoryList(Long categoryId) {
        List<BaseCategoryView> rows = baseCategoryViewMapper.selectList(
                new LambdaQueryWrapper<>(BaseCategoryView.class)
                        .eq(ObjectUtil.isNotEmpty(categoryId), BaseCategoryView::getCategory1Id, categoryId));
        Map<Long, AlbumCategoryVo> l1Map = new LinkedHashMap<>();
        Map<Long, AlbumCategoryVo> l2Map = new LinkedHashMap<>();

        for (BaseCategoryView row : rows) {
            // L1
            AlbumCategoryVo l1Vo = l1Map.computeIfAbsent(row.getCategory1Id(), id -> {
                AlbumCategoryVo vo = new AlbumCategoryVo();
                vo.setCategoryId(id);
                vo.setCategoryName(row.getCategory1Name());
                vo.setCategoryChild(new ArrayList<>());
                return vo;
            });

            // L2
            AlbumCategoryVo l2Vo = l2Map.computeIfAbsent(row.getCategory2Id(), id -> {
                AlbumCategoryVo vo = new AlbumCategoryVo();
                vo.setCategoryId(id);
                vo.setCategoryName(row.getCategory2Name());
                vo.setCategoryChild(new ArrayList<>());
                l1Vo.getCategoryChild().add(vo);
                return vo;
            });

            // L3
            AlbumCategoryVo l3Vo = new AlbumCategoryVo();
            l3Vo.setCategoryId(row.getCategory3Id());
            l3Vo.setCategoryName(row.getCategory3Name());
            l2Vo.getCategoryChild().add(l3Vo);
        }
        // 返回第一个一级分类（因为按category1Id查询，只有一个）
        List<AlbumCategoryVo> result = new ArrayList<>(l1Map.values());
        return result.isEmpty() ? new AlbumCategoryVo() : result.get(0);
    }

    /**
     * 描述：查询所有一级分类
     *
     * @return
     * @author daiyuling
     * @date 2025/10/20
     */
    @Override
    public List<BaseCategory1> findAllCategory1() {
        return baseCategory1Mapper.selectList(null);
    }

    /**
     * 描述：根据一级分类id查询三级分类信息
     *
     * @param categoryId
     * @return
     * @author daiyuling
     * @date 2025/10/20
     */
    @Override
    public List<BaseCategory3> findTopBaseCategory3(Long categoryId) {
        return baseCategory1Mapper.findTopBaseCategory3(categoryId);
    }

    /**
     * 描述：根据一级分类Id查询属性
     *
     * @param category1Id
     * @return
     * @author daiyuling
     * @date 2025/10/20
     */
    @Override
    public List<BaseAttribute> findAttribute(Long category1Id) {
        return baseCategory1Mapper.findAttribute(category1Id);
    }

    /**
     * 描述：根据三级分类id获取分类
     *
     * @param category3Id
     * @return
     * @author daiyuling
     * @date 2025/10/20
     */
    @Override
    public BaseCategoryView getCategoryView(Long category3Id) {
        return baseCategoryViewMapper.selectOne(new LambdaQueryWrapper<>(BaseCategoryView.class)
                .eq(BaseCategoryView::getCategory3Id, category3Id));
    }
}
